<template>
  <div class="app-container">
    <div class="menu-container">
      <el-form :inline="true" style="float: left;">
        <el-button size="mini" type="primary" @click="addNewSerie()">添加系列</el-button>
      </el-form>
    </div>
    <el-table v-loading="listLoading" :data="page.records" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="系列名">
        <template slot-scope="scope">
          <el-button type="text" size="small" icon="el-icon-edit" @click="modifySerie(scope.row)">
            {{ scope.row.name }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="状态">
        <template slot-scope="scope">
          <span>{{ scope.row.state_desc }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" align="center" label="显示顺序">
        <template slot-scope="scope">
          <span>{{ scope.row.show_order }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Actions" width="240">
        <template slot-scope="scope">
          <!-- <el-button type="primary" size="small" icon="el-icon-edit" @click="modifySerie(scope.row)">修改</el-button> -->
          <el-button type="danger" size="small" icon="el-icon-delete" @click="delSerie(scope.row)">删除</el-button>
          <el-button type="primary" size="small" icon="el-icon-tickets" @click="managerSerieVideo(scope.row)">系列单品管理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="page.recordsTotal>0" :total="page.recordsTotal" :page.sync="page.pageNum" :page-sizes="[50, 100]" :limit.sync="page.pageSize" @pagination="getList" />

    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">

      <el-form :model="serieForm">
        <el-form-item :label-width="formLabelWidth" label="系列名称">
          <el-input v-model="serieForm.name" auto-complete="off" size="mini"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="排序">
          <el-input-number v-model="serieForm.show_order" :min="1" :max="100" size="mini" label="排序"/>
          <span>排序值越小排序越靠前</span>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="状态">
          <el-select v-model="serieForm.state" placeholder="请选择" size="mini">
            <el-option
              v-for="item in stateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible = false">取 消</el-button>

        <el-button type="primary" size="mini" @click="createOrUpdateSerie()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogSerieVideoVisible" title="系列单品管理管理">
      <el-form :model="optionForm">
        <el-form-item style="margin-bottom: 10px;" label="封面图">
          <Upload v-model="optionForm.img_url" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="管理单品">
          <el-select v-model="optionForm.video_id" filterable placeholder="请选择" size="mini">
            <el-option
              v-for="item in videoOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="排序">
          <el-input-number v-model="optionForm.show_order" :min="1" :max="100" size="mini" label="排序"/>
          <span>排序值越小排序越靠前</span>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="状态">
          <el-select v-model="optionForm.state" placeholder="请选择" size="mini">
            <el-option
              v-for="item in stateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth">
          <template v-if="isEditSerie">
            <el-button type="primary" size="mini" @click="createSerieVideo()">确定修改</el-button>
            <el-button type="primary" size="mini" @click="resetOptionForm()">清空新增</el-button>
          </template>
          <el-button v-if="!isEditSerie" type="primary" size="mini" @click="createSerieVideo()">新增系列单品</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="listLoading" :data="optionsPage.records" border fit highlight-current-row style="width: 100%" height="280">
        <el-table-column prop="id" label="ID" align="center" width="80"/>
        <el-table-column prop="serie_name" label="系列名" align="center"/>
        <el-table-column prop="video_title" label="视频名" align="center" width="140"/>
        <el-table-column prop="state_desc" label="状态" align="center" width="80"/>
        <el-table-column prop="show_order" label="显示顺序" align="center" width="80"/>
        <el-table-column prop="create_time" label="创建时间" align="left" width="100"/>
        <el-table-column prop="" label="操作" align="center" width="140">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="modifyCategoryOption(scope.row)">修改</el-button>
            <el-button size="small" type="text" @click="delSerieOption(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="optionsPage.recordsTotal>0" :total="optionsPage.recordsTotal" :page-sizes="[50, 100]" :limit.sync="optionsPage.pageSize" :options-pagesync.="optionsPage.pageNum" @pagination="getListVideoOptions" />
    </el-dialog>

  </div>
</template>

<script>
import { fetchList, createSerie, updateSerie, fetchSerieVideoList, createSerieVideo, updateSerieVideo, deleteSerie, deleteSerieVideo } from '@/api/serie'
import { fetchVideoOptions } from '@/api/video'
import Upload from '@/components/Upload/singleImage3'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'SerieList',
  components: { Pagination, Upload },
  data() {
    return {
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      currentSerie: undefined,
      formLabelWidth: '100px',
      dialogFormVisible: false,

      dialogSerieVideoVisible: false,
      dialogTitle: '新增系列',
      isEditSerie: false,
      serieForm: { 'id': undefined, 'name': '', 'show_order': 1, 'state': 0 },
      stateOptions: [{ 'label': '有效', 'value': 0 }, { 'label': '无效', 'value': 1 }],
      videoOptions: [],
      optionForm: { 'id': undefined, 'show_order': 1, 'state': 0, 'img_url': '', 'serie_id': -1, 'video_id': '' },
      optionsPage: { pageNum: 1, pageSize: 100, pageTotal: 0, recordsTotal: 0, records: [] },
      page: { pageNum: 1, pageSize: 100, pageTotal: 0, recordsTotal: 0, records: [] }
    }
  },
  created() {
    this.getList()
    this.getListVideoOptions()
  },
  methods: {
    clearSerieForm() {
      this.serieForm = { 'id': undefined, 'name': '', 'show_order': 1, 'state': 0 }
    },
    clearOptionForm() {
      this.optionForm = { 'id': undefined, 'show_order': 1, 'state': 0, 'img_url': '', 'serie_id': -1, 'video_id': '' }
    },
    resetOptionForm() {
      this.clearOptionForm()
      this.isEditSerie = false
      this.optionForm.serie_id = this.currentSerie.id
    },
    addNewSerie() {
      this.dialogTitle = '新增系列'
      this.dialogFormVisible = true
    },
    modifySerie(serie) {
      this.serieForm.id = serie.id
      this.serieForm.name = serie.name
      this.serieForm.show_order = serie.show_order
      this.serieForm.state = serie.state
      this.dialogTitle = '修改系列'
      this.dialogFormVisible = true
    },
    managerSerieVideo(serie) {
      this.clearOptionForm()
      // 记录当前的系列数据
      this.currentSerie = serie
      this.optionForm.serie_id = this.currentSerie.id
      this.dialogSerieVideoVisible = true
      // 查询系列下的单品数据
      this.getSerieVideos()
    },
    createOrUpdateSerie() {
      let msg = '更新成功'
      let reqestFunc = updateSerie
      if (this.serieForm.id === undefined || this.serieForm.id === '') {
        msg = '创建成功'
        reqestFunc = createSerie
      }
      reqestFunc(this.serieForm).then(response => {
        const r = response.data
        if (r.code === 0) {
          this.$message({ message: msg, type: 'success' })
          this.dialogFormVisible = false

          this.clearSerieForm()
          this.getList()
        } else {
          this.$message({ message: r.msg, type: 'error' })
        }
      })
    },
    createSerieVideo() {
      let msg = '更新成功'
      let reqestFunc = updateSerieVideo
      if (this.optionForm.id === undefined || this.optionForm.id === '') {
        msg = '创建成功'
        reqestFunc = createSerieVideo
      }
      reqestFunc(this.optionForm).then(response => {
        const r = response.data
        if (r.code === 0) {
          this.$message({ message: msg, type: 'success' })
          this.dialogFormVisible = false

          this.clearOptionForm()
          this.getSerieVideos()
        } else {
          this.$message({ message: r.msg, type: 'error' })
        }
      })
    },
    modifyCategoryOption(serieVideo) {
      console.log(serieVideo)
      this.isEditSerie = true
      this.optionForm.id = serieVideo.id
      this.optionForm.title = serieVideo.title
      this.optionForm.show_order = serieVideo.show_order
      this.optionForm.state = serieVideo.state
      this.optionForm.serie_id = serieVideo.serie_id
      this.optionForm.video_id = serieVideo.video_id
      this.optionForm.img_url = serieVideo.img_url
      this.optionForm.desc = serieVideo.desc
    },
    delSerie(category) {
      const data = { id: category.id, type: 'serie' }
      this.myConfirm('即将删除系列, 是否继续?', this._delSerie, data, '已取消操作')
    },
    delSerieOption(cateOption) {
      const data = { id: cateOption.id, type: 'serieVideo' }
      this.myConfirm('即将删除系列单品, 是否继续?', this._delSerie, data, '已取消操作')
    },
    _delSerie(data) {
      let requestFunc = deleteSerie
      let responseCallback = this.getList
      if (data.type === 'serieVideo') {
        requestFunc = deleteSerieVideo
        responseCallback = this.getSerieVideos
      }
      requestFunc(data.id).then(response => {
        const r = response.data
        if (r.code === 0) {
          this.$message({ message: '操作成功', type: 'success' })
          responseCallback()
        } else {
          this.$message({ message: r.msg, type: 'error' })
        }
      })
    },
    getSerieVideos() {
      if (this.currentSerie == null || this.currentSerie === undefined) {
        return
      }
      fetchSerieVideoList({
        serie_id: this.currentSerie.id,
        pageSize: this.page.page_size,
        pageNum: this.page.page_num
      }).then(response => {
        const r = response.data
        console.log(r.data)
        if (r.code === 0) {
          this.optionsPage = r.data
        } else {
          this.$message.error('服务端出错了哦')
        }
      })
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
        }
        this.page = data.data
        this.listLoading = false
      })
    },
    getListVideoOptions() {
      this.listLoading = true
      fetchVideoOptions().then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
        }
        this.videoOptions = data.data.videos
        this.listLoading = false
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
